import { Injectable } from '@angular/core';
import {
  HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';

import { Observable } from 'rxjs';

/** Pass untouched request through to the next request handler. */
@Injectable()
export class GlobalInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
        console.log("所有的请求都将经过这里")
        const token = window.localStorage.getItem('auth_token')
        let authReq = req
        if(!(token === null)){
            authReq = req.clone({
                headers: req.headers.set('X-Access-Token', token)
              });
        }
        
    return next.handle(authReq);
  }
}